/**
 * 描述:  统一的缓存控制,可以控制js和css的缓存以及动态写入css和js
 */
(function (exports) {
    "use strict";
    //设置下seaConfig全局配置文件,后面加上后缀防止冲突
      var rand=localStorage.getItem("setver")==null?Math.random():"";
      //var rand=Math.random();
    exports.cacheConfig_Ray = {
        //这里设置一个全局时间戳缓存,用来缓存js和css的有效时间
        //Ray框架中也会默认使用这个参数
        //TIME_STAMP: 'r=2'+localStorage.getItem("setver")
        TIME_STAMP: 'r=298'+localStorage.getItem("setver")+rand
    };
	
    //文件写入
    exports.SrcBoot = {
        //得到文件的相对路径
        getRealativePath: function (path) {
            // 全路径
            if (/^(http|https|ftp)/g.test(path)) {
                return path;
            }
            // 是否是相对路径
            var isRelative = path.indexOf('./') != -1 || path.indexOf('../') != -1;
            if (!isRelative) {
                path = '../'+path;
            }
            return path;
        },
        //得到文件后缀
        getPathSuffix: function (path) {
            var dotPos = path.lastIndexOf('.'),
                    suffix = path.substring(dotPos + 1);
            return suffix;
        },
        // 批量输出css|js
        output: function (arr) {
            var i = 0,
                    len = arr.length,
                    path,
                    ext;
			lengths=len;
            for (; i < len; i++) {
                path = this.getRealativePath(arr[i]);
                ext = this.getPathSuffix(path);

                //统一加上时间戳缓存
                if (path.indexOf('?') === -1) {
                    //没有?,加上？
                    path += '?';
                } else {
                    //有了?,加上&
                    path += '&';
                }
                path += exports.cacheConfig_Ray.TIME_STAMP;

                if (ext == 'js') {
                	var oScript= document.createElement("script");
				    oScript.type = "text/javascript";
				    oScript.src=path;
                	document.getElementsByTagName('HEAD').item(0).appendChild(oScript);
                	oScript.onload=function(){addnums++; bidui();}
                }else if(ext == 'html'){
                	$.ajax({ 
					  type:"get", 
					  url:path, 
					  dataType:"html", 
					  success:function(msg){ 
					    tplhtml+=msg
					    addnums++; bidui();
					  }
					})
                }else if(ext == 'json'){
                	var sn1=path.split(".json")[0],
                		sn2=sn1.substr(sn1.length - 2);
						cachejson(path,sn2)
                } else {
                	var oCss= document.createElement("link");
				    oCss.rel = "stylesheet";
				    oCss.href=path;
                    document.getElementsByTagName('HEAD').item(0).appendChild(oCss);
                    oCss.onload=function(){addnums++; bidui();}
                }
            }
        },
        outSeaConfig: function () {
            this.output([
                //写入每个页面必备的seajs配置

            ]);
        }
    };
})(window);
var lengths=0,
	addnums=0;
//比对
function bidui(){
	var sn=Math.floor((addnums/lengths)*100)
		document.getElementById('progress-bar').style.width=sn+"%";
	if(lengths==addnums){
		//执行
		setTimeout(function(){
			$("body").removeClass("pace")
			$(".loading").remove();
			main();
		},500)
	}
}

//设置语言json
function cachejson(path,j){
	var sn2=j;
	tpljson[sn2]={};
	$.ajax({ 
	  type:"get", 
	  url:path, 
	  dataType:"json", 
	  success:function(msg){ 
	    tpljson[sn2]=msg;
	    addnums++; bidui();
	    //console.log(tpljson)
	  }
	})
}



var tplhtml = "", //全局tplhtml
	tpljson = {}  //全局JSON